/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.contorller;

import com.apache.api.vo.ResultMsg;
import com.apache.client.InfoReleaseCoreCleint;
import com.apache.portal.common.enums.InfoMethodEnum;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.tools.ConfigUtil;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * description:
 *
 * @author Hou Dayu 创建时间：2016-8-29
 */
@SuppressWarnings("serial")
public class InfoServlet extends BaseHttpServlet {

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     */
    protected void doInvoke(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String methodKey = request.getParameter("doCode");
        LoginUser loginUser = this.getSessionUser(request);
        String result = "";
        ResultMsg msg = null;
        Map<String, String> params = this.getParameterMap(request);
        String sysPass = ConfigUtil.getInstance().interfacePass();
        params.put("sysPass", sysPass);
        json2map1(params, request, loginUser);//动态参数
        switch (InfoMethodEnum.getName(methodKey)) {//为ajax方式调用(主要为网站使用)
        case Subcount:
            params.put("createUser", loginUser.getUserEname());
            long scount = InfoReleaseCoreCleint.getInstance().countMasterSlaveInfo(params);
            result = scount + "";
            break;
        case Count://获取信息总数(createUser,sortId可空)
            params.put("createUser", loginUser.getUserEname());
            long count = InfoReleaseCoreCleint.getInstance().countStatistics(params);
            result = count + "";
            break;
        case List:
            Object objs = InfoReleaseCoreCleint.getInstance().selectInfo(params);
            result = getQuery(objs);
            break;
        case Pages:
            params.put("pageSize", StrUtil.doNull(params.get("pageSize"), "10"));
            params.put("pageIndex", StrUtil.doNull(params.get("pageIndex"), "1"));
            Object page = InfoReleaseCoreCleint.getInstance().selectInfo(params);
            result = getQuery(page);
            break;
        case View:
            Object obj = InfoReleaseCoreCleint.getInstance().selectSingle(params);
            result = getQuery(obj);
            break;
        case Sublist:
            Object subObjs = InfoReleaseCoreCleint.getInstance().selectMasterSlaveInfo(params);
            result = getQuery(subObjs);
            break;
        case Subpages:
            params.put("pageSize", StrUtil.doNull(params.get("pageSize"), "10"));
            params.put("pageIndex", StrUtil.doNull(params.get("pageIndex"), "1"));
            Object subPage = InfoReleaseCoreCleint.getInstance().selectMasterSlaveInfo(params);
            result = getQuery(subPage);
            break;
        case Subview:
            Object subobj = InfoReleaseCoreCleint.getInstance().selectMasterSlaveSingle(params);
            result = getQuery(subobj);
            break;
        //验证是否曾经意向登记过（判重）
        case Check:
            Object dubmsg = InfoReleaseCoreCleint.getInstance().selectInfo(params);
            ResultMsg rmsgd = new ResultMsg("F", "可以意向登记");
            if (dubmsg != null && ((List) dubmsg).size() > 0) {
                rmsgd = new ResultMsg("T", "已经意向登记过");
            }
            result = JSONObject.fromObject(rmsgd).toString();
            break;
        }
        if (StrUtil.isNull(result)) {//以下主要实现增删改的操作
            Object msg1 = PortalPubFactory.getInstance().getPlugin("info")
                    .doInvoke(request, response, params);
            if (null == msg1) {
                msg1 = new ResultMsg("F", "操作失败");
            }
            result = JSONObject.fromObject(msg1).toString();
        }
        outputJson(result, response);
    }

    private String getQuery(Object obj) {
        if (null != obj) {
            return JSONObject.fromObject(obj).toString();
        } else {
            return "{}";
        }
    }

    /**
     * json转map
     */
    private void json2map1(Map<String, String> params, HttpServletRequest request,
            LoginUser loginUser) {
        if (StrUtil.isNotNull(request.getParameter("sqlQuery"))) {
            JSONObject jsonObject = JSONObject.fromObject(request.getParameter("sqlQuery"));
            Map<String, String> map1 = JSONObject.fromObject(jsonObject);
            String selectUser = String.valueOf(map1.get("selectUser"));
            if ("true".equals(selectUser) && null != loginUser) {
                params.put("createUser", loginUser.getUserEname());
            }
            map1.remove("selectUser");
            params.putAll(map1);
        } else {
            if (null != loginUser) {
                params.put("createUser", loginUser.getUserEname());
            }
        }
    }
}
